package fz.vrd.library.page.fragment;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import fz.vrd.library.log.LogFactory;

 /**
  * @类说明：在fragment中使用
  * @创建者： VinRichard  2025/3/26/026
  * @备注：
  */
public class BaseFrg extends Fragment {


     /**
      * 加载根 fragment
      *
      * @param containerId
      * @param rootfragment
      * @param fragmentTag
      */
     public void loadRootfragment(int containerId, @NonNull Fragment rootfragment, String fragmentTag) {
         getChildFragmentManager()
                 .beginTransaction()
                 .setReorderingAllowed(true)
                 .add(containerId, rootfragment, fragmentTag)
                 .commitAllowingStateLoss();
     }

    protected void addAndCommit(Fragment fragment, FragmentTransaction transaction) {
        addAndCommit(fragment, fragment.getClass().getSimpleName(), transaction);
    }

    protected void addAndCommit(Fragment fragment, String tag, FragmentTransaction transaction) {
        transaction.add(fragment, tag)
                .addToBackStack(tag)
                .commit();
    }

    protected void replace(@IdRes int containerViewId, Fragment fragment, FragmentTransaction transaction) {
        transaction.replace(containerViewId, fragment)
                .addToBackStack(fragment.getClass().getName())
                .commit();
    }

     protected void replace(Fragment fragment) {
         FragmentManager fragmentManager = getParentFragmentManager();
         fragmentManager
                 .beginTransaction()
                 .replace(fragmentManager.getFragments().get(0).getId(), fragment)
                 .addToBackStack(fragment.getClass().getName())
                 .commit();
     }


    protected void remove(Fragment fragment, FragmentTransaction transaction) {
        transaction.remove(fragment).commit();
    }

    protected void backPage() {
        FragmentManager fragmentManager = getChildFragmentManager();
        if(fragmentManager.getFragments() == null || fragmentManager.getFragments().size() == 0){
            fragmentManager = getParentFragmentManager();
        }

        int stackCount = fragmentManager.getBackStackEntryCount();

        if(stackCount == 0  && fragmentManager != getActivity().getSupportFragmentManager()){
            fragmentManager = getActivity().getSupportFragmentManager();
            stackCount = fragmentManager.getBackStackEntryCount();
        }

        if(stackCount > 0){
            fragmentManager.popBackStack();
            fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                @Override
                public void onBackStackChanged() {

                }
            });
        }else if(fragmentManager == getActivity().getSupportFragmentManager()){
            getActivity().finish();
        }
    }


}
